﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao
{
    public class IPProducto : Intermediario
    {
        public override List<T> getAll<T>()
        {
            return (from prod in dao.Productos where prod.fechaBaja == null && prod.Activo == true orderby prod.NombreProducto select prod).ToList() as List<T>;
        }

        public override List<T> getByCriterio<T>(string att, string op, string val)
        {
			return dao.ExecuteQuery<Producto>("SELECT * FROM Producto WHERE Producto." + att + " " + op + " '" + val + "' AND fechaBaja IS NULL ORDER BY Producto.NombreProducto").ToList() as List<T>;
            //return (from prod in dao.Productos where prod.fechaBaja != null && prod.cantidad <= prod.MinimoStockProducto orderby prod.NombreProducto select prod).ToList() as List<T>;
        }

        public override IList<P> getCriterioById<P>(string att, string op, int val)
        {
            return dao.ExecuteQuery<Producto>("SELECT * FROM Producto WHERE Producto." + att + " " + op + " '" + val + "' ORDER BY Producto.NombreProducto").ToList() as List<P>;
        }

        public override IList<T> getPorCriterio<T>(string name)
        {
            return (from p in dao.Productos
                    where p.NombreProducto == name
                    select p).ToList() as List<T>; //.SingleOrDefault() as List<T>; //retorna un solo Producto
        }

        public override void save<T>(T entity)
        {
            try
            {
                Producto pr = entity as Producto;
                pr.Activo = true;
                dao.Productos.InsertOnSubmit(pr);
            }
            catch (Exception ex)
            {
                throw new Exception("IPRProveedor.save - " + ex.Message);
            }
        }

        public override object getPorId(int id)
        {
			IList<Producto> prods = (from p in dao.Productos where p.IdProducto == id select p).ToList();

			return (prods.Count > 0 ? prods.First() : null);
        }

        public override void Delete(int id)
        {
            throw new NotImplementedException();
        }

        public override void Update<T>(T entity)
        {
            throw new NotImplementedException();
        }
    }
}
